package service_logger

import (
	"context"
)

func (a *LoggerService) StartCronJob(ctx context.Context) {
	a.cronJobSvc.MustAddFunc(ctx, "@every 1m", func() {
		a.limitLogRecordByMaxSaveCount(ctx)
	})
	a.cronJobSvc.MustAddFunc(ctx, "@every 3m", func() {
		a.limitLogRecordByMaxSaveDuration(ctx)
	})
}

func (a *LoggerService) limitLogRecordByMaxSaveCount(ctx context.Context) {
	loggers, err := a.moduleAccount.GetAllLoggerSelective(ctx)
	if err != nil {
		a.log.Info(err)
	}
	for i := range loggers {
		_, err := a.moduleLog.RealDeleteLogByMaxSaveCount(ctx, loggers[i].Id, loggers[i].MaxSaveCount)
		if err != nil {
			a.log.Infof("%v", err)
		}
	}
}
func (a *LoggerService) limitLogRecordByMaxSaveDuration(ctx context.Context) {
	loggers, err := a.moduleAccount.GetAllLoggerSelective(ctx)
	if err != nil {
		a.log.Info(err)
	}
	for i := range loggers {
		_, err := a.moduleLog.RealDeleteLogByMaxSaveDuration(ctx, loggers[i].Id, loggers[i].MaxSaveDuration)
		if err != nil {
			a.log.Infof("%v", err)
		}
	}
}
